package com.nanhai.buddhist.college.module.study.service.studyadditionalcourses;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.study.controller.admin.studyadditionalcourses.vo.*;
import com.nanhai.buddhist.college.module.study.convert.studyadditionalcourses.StudyAdditionalCoursesStudentConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesStudentDO;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyadditionalcourses.StudyAdditionalCoursesStudentMapper;
import com.nanhai.buddhist.college.module.study.enums.additionalCourses.StudentCoursesApplyEnums;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 学生选课 Service 实现类
 *
 * @author xqenzo@163.com
 */
@Service
@Validated
public class StudyAdditionalCoursesStudentServiceImpl implements StudyAdditionalCoursesStudentService {

    @Resource
    private StudyAdditionalCoursesStudentMapper additionalCoursesStudentMapper;


    @Resource
    private StudyAdditionalCoursesService studyAdditionalCoursesService;

    @Override
    public String createAdditionalCoursesStudent(StudyAdditionalCoursesStudentCreateReqVO createReqVO) {


        // 判断课程库存

        StudyAdditionalCoursesDO additionalCourses = studyAdditionalCoursesService.getAdditionalCourses(createReqVO.getStudyAdditionalCoursesId());

        if (Objects.isNull(additionalCourses)) {
            throw exception(ADDITIONAL_COURSES_NOT_EXISTS);
        }

        Integer limitStudentCount = additionalCourses.getLimitStudentCount();

        List<Integer> authList = ListUtil.of(StudentCoursesApplyEnums.APPLIED.getType(), StudentCoursesApplyEnums.COMPLETED.getType());
        LambdaQueryWrapper<StudyAdditionalCoursesStudentDO> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.in(StudyAdditionalCoursesStudentDO::getAuditStatus, authList);
        objectLambdaQueryWrapper.eq(StudyAdditionalCoursesStudentDO::getStudyAdditionalCoursesId, createReqVO.getStudyAdditionalCoursesId());
        Long count = additionalCoursesStudentMapper.selectCount(objectLambdaQueryWrapper);
        if (count >= limitStudentCount) {
            throw exception(ADDITIONAL_COURSES_OVER);   // 课程已选完
        }


        StudyAdditionalCoursesStudentDO additionalCoursesStudent = StudyAdditionalCoursesStudentConvert.INSTANCE.convert(createReqVO);
        StudyAdditionalCoursesStudentDO studyAdditionalCoursesStudentDO = additionalCoursesStudentMapper.selectOne(StudyAdditionalCoursesStudentDO::getStudentId, createReqVO.getStudentId(), StudyAdditionalCoursesStudentDO::getStudyAdditionalCoursesId, createReqVO.getStudyAdditionalCoursesId());
        //学生第一次选课
        if (Objects.isNull(studyAdditionalCoursesStudentDO)) {
            additionalCoursesStudent.setAuditStatus(StudentCoursesApplyEnums.APPLIED.getType());
            additionalCoursesStudentMapper.insert(additionalCoursesStudent);
        } else {
            // 申请中和审核完成不能再次提交
            if (studyAdditionalCoursesStudentDO.getAuditStatus().equals(StudentCoursesApplyEnums.APPLIED.getType()) || studyAdditionalCoursesStudentDO.getAuditStatus().equals(StudentCoursesApplyEnums.COMPLETED.getType())) {
                throw exception(ADDITIONAL_COURSES_STUDENT_APPLIED);
            }
            studyAdditionalCoursesStudentDO.setAuditStatus(StudentCoursesApplyEnums.APPLIED.getType());
            additionalCoursesStudentMapper.updateById(studyAdditionalCoursesStudentDO);
        }

        // 返回
        return additionalCoursesStudent.getId();
    }

    @Override
    public void updateAdditionalCoursesStudent(StudyAdditionalCoursesStudentUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateAdditionalCoursesStudentExists(updateReqVO.getId());
        // 更新
        StudyAdditionalCoursesStudentDO updateObj = StudyAdditionalCoursesStudentConvert.INSTANCE.convert(updateReqVO);
        additionalCoursesStudentMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdditionalCoursesStudent(String id) {
        // 校验存在
        this.validateAdditionalCoursesStudentExists(id);
        // 删除
        additionalCoursesStudentMapper.deleteById(id);
    }

    private void validateAdditionalCoursesStudentExists(String id) {
        if (additionalCoursesStudentMapper.selectById(id) == null) {
            throw exception(ADDITIONAL_COURSES_STUDENT_NOT_EXISTS);
        }
    }

    @Override
    public StudyAdditionalCoursesStudentDO getAdditionalCoursesStudent(String id) {
        return additionalCoursesStudentMapper.selectById(id);
    }

    @Override
    public List<StudyAdditionalCoursesStudentDO> getAdditionalCoursesStudentList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : additionalCoursesStudentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudyAdditionalCoursesStudentDO> getAdditionalCoursesStudentPage(StudyAdditionalCoursesStudentPageReqVO pageReqVO) {
        return additionalCoursesStudentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyAdditionalCoursesStudentDO> getAdditionalCoursesStudentList(StudyAdditionalCoursesStudentExportReqVO exportReqVO) {
        return additionalCoursesStudentMapper.selectList(exportReqVO);
    }

    @Override
    public String backAdditionalCoursesByStudent(StudyAdditionalCoursesStudentCreateReqVO createReqVO) {

        StudyAdditionalCoursesStudentDO studyAdditionalCoursesStudentDO = additionalCoursesStudentMapper.selectOne(StudyAdditionalCoursesStudentDO::getStudentId, createReqVO.getStudentId(), StudyAdditionalCoursesStudentDO::getStudyAdditionalCoursesId, createReqVO.getStudyAdditionalCoursesId());
        if (Objects.isNull(studyAdditionalCoursesStudentDO)) {
            throw exception(ADDITIONAL_COURSES_STUDENT_NOT_EXISTS);
        }


        studyAdditionalCoursesStudentDO.setAuditStatus(StudentCoursesApplyEnums.BACK_COURSES.getType());
        additionalCoursesStudentMapper.updateById(studyAdditionalCoursesStudentDO);

        return studyAdditionalCoursesStudentDO.getId();
    }

    @Override
    public String auditAdditionalCoursesByStudent(StudyAdditionalCoursesStudentAuditReqVO createReqVO) {
        StudyAdditionalCoursesStudentDO studyAdditionalCoursesStudentDO = additionalCoursesStudentMapper.selectById(createReqVO.getId());
        if (studyAdditionalCoursesStudentDO == null) {
            throw exception(ADDITIONAL_COURSES_STUDENT_NOT_EXISTS);
        }

        studyAdditionalCoursesStudentDO.setAuditStatus(createReqVO.getAuditStatus());
        studyAdditionalCoursesStudentDO.setRemark(createReqVO.getRemark());

        additionalCoursesStudentMapper.updateById(studyAdditionalCoursesStudentDO);

        return studyAdditionalCoursesStudentDO.getId();
    }
}
